import java.util.Arrays;

public class MyStack {
    private int[] elem;
    private int usedSize;

    private static final int DEFAULT_CAPACITY = 10;

    public MyStack() {
        this.elem = new int[DEFAULT_CAPACITY];
    }

    public MyStack(int capacity) {
        this.elem = new int[capacity];
    }

    private void checkFull() {
        if (usedSize == elem.length) {
            elem = Arrays.copyOf(this.elem, this.usedSize * 2);
        }
    }

    public void push(int val) {
        checkFull();//检查并扩容
        this.elem[this.usedSize] = val;
        this.usedSize++;
    }

    public boolean isEmpty() {
        return usedSize == 0;
    }

    public int pop() {
        if (isEmpty()) {
            throw new EmptyException("栈为空，不能pop！");
        }
        return this.elem[--usedSize];
    }

    public int peek() {
        return this.elem[usedSize - 1];
    }
    
}
